package com.itfreer.data.organize.dictionary;

import com.itfreer.data.SysDatabaseHelper;
import com.itfreer.data.dictionary.TableDictionaryBase;
import com.itfreer.data.dictionary.base.DictionaryItem;
import com.itfreer.data.organize.entity.OrganizationEntity;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定义机构字典表
 */
public class OrganizationDictionary extends TableDictionaryBase {

    private static final String dn = "p_organization";

    @Override
    public String getText(String dicName, String id) {
        dicName = dicName.toLowerCase().trim();
        if(!dicName.equals(dn)){
            return null;
        }

        try {
            Dao<OrganizationEntity, String> dao = SysDatabaseHelper.getInstance().getDao(OrganizationEntity.class);
            OrganizationEntity entity = dao.queryForId(id);
            if(entity==null){
                return "";
            }else{
                return entity.getDisplay();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getDictionaryString(String dicName) {
        return null;
    }

    @Override
    public DictionaryItem getDictionary(String dicName, String id) {
        dicName = dicName.toLowerCase().trim();
        if(!dicName.equals(dn)){
            return null;
        }

        return null;
    }

    @Override
    public List<DictionaryItem> getChild(String dicName, String id, Boolean getNoValidValue) {
        dicName = dicName.toLowerCase().trim();
        if(!dicName.equals(dn)){
            return null;
        }

        try {
            Dao<OrganizationEntity, String> dao = SysDatabaseHelper.getInstance().getDao(OrganizationEntity.class);
            Map<String, Object> fieldValues = new HashMap<>();
            fieldValues.put("pid", id);
            if(!getNoValidValue){
                fieldValues.put("valid", true);
            }
            List<OrganizationEntity> items = dao.queryForFieldValues(fieldValues);
            if(items==null || items.size()<=0){
                return null;
            }
            items.sort(new Comparator<OrganizationEntity>() {
                @Override
                public int compare(OrganizationEntity o1, OrganizationEntity o2) {
                    return o1.getOrder().compareTo(o2.getOrder());
                }
            });
            List<DictionaryItem> dicList = new ArrayList<>();
            for(OrganizationEntity item : items){
                DictionaryItem dic = new DictionaryItem();
                dic.setAbbreviation(item.getAbbreviation());
                dic.setCode(item.getCode());
                dic.setDisplay(item.getDisplay());
                dic.setId(item.getId());
                dic.setInfo(item.getInfo());
                dic.setLeaf(item.getLeaf());
                dic.setLevel(item.getLevel());
                dic.setOrder(item.getOrder());
                dic.setPid(item.getPid());
                dic.setValid(item.getValid());
                dicList.add(dic);
            }
            return dicList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
